<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台品牌管理控制器
 * @author 明明 <zhumingming_12346@163.com>
 */
class BrandController extends AdminController {
    /***
     * 商品规格列表
     */
    public function  speci(){
        $this->meta_title='商品规格管理';
       $info= M('Spec')->select();
        foreach($info as $k=>$v){
            $info[$k]['cate']=M('GoodsType')->where(array('id'=>$v['type_id']))->getField('name');
            $info[$k]['item']=M('spec_item')->where(array('spec_id'=>$v['id']))->select();
            $info[$k]['its']=array_column($info[$k]['item'],'item');
            $info[$k]['it']=implode(',',$info[$k]['its']);
        }
        int_to_string($info);
        $this->assign('list',$info);
        $this->display();
    }
    /***
     * 商品规格添加
     */
    public function  aspeci(){
        $this->meta_title='商品规格添加';
        $Goods = D('Spec');
        if(IS_POST){
            if(empty($_POST['name'])){
                $this->error('规格名称不能为空');
            }
            $data['name']=$_POST['name'];
            $data['type_id']=$_POST['type_id'];
             $info= M('Spec')->add($data);
            $indo= $Goods->afterSave($info);
            if(empty($info) && empty($indo)){
                $this->error('商品规格添加失败,请重试');
            }else{
                $this->success('商品规格添加成功',U('Brand/speci'));
            }
        }else{
           $goods=M('GoodsType')->select();
            $this->assign('goods',$goods);
        }
        $this->display();
    }


    /***
     *  商品规格 编辑
     */
    public function edspeci(){
        $id=I('id');
        $Goods = D('Spec');
            if(IS_POST){
                $data['name']=$_POST['name'];
                $data['type_id']=$_POST['type_id'];
                $info= M('Spec')->where(array('id'=>$_POST['id']))->save($data);
                $indo= $Goods->afterSave($_POST['id']);
                if(empty($info) && empty($indo)){
                    $this->error('商品规格编辑失败,请重试');
                }else{
                    $this->success('商品规格编辑成功',U('Brand/speci'));
                }
            }else{
                if(empty($id)){
                    $this->error('参数错误,请重试');
                }else{
                    $this->meta_title='编辑商品规格';
                   $info= M('Spec')->where(array('id'=>$id))->find();
                    $info_s= M('spec_item')->where(array('spec_id'=>$info['id']))->field('item')->select();
                      $info_a=array_column($info_s,'item');
                    $info['item'] = implode(PHP_EOL, $info_a);
                    $goods=M('GoodsType')->select();
                    $this->assign('info',$info);
                    $this->assign('goods',$goods);
                }
            $this->display('aspeci');
        }
    }
        /**
         * 删除商品规格
         */
        public function delGoodsSpec()
        {
            // 判断 商品规格项
            $count = M("SpecItem")->where("spec_id = {$_GET['id']}")->count("1");
            $count > 0 && $this->error('清空规格项后才可以删除!',U('Admin/Goods/specList'));
            // 删除分类
            M('Spec')->where("id = {$_GET['id']}")->delete();
            $this->success("操作成功!!!",U('Brand/speci'));
        }
}
